package com.course.business.controller.admin;

import com.course.result.ResponseDto;
import com.course.result.ResultController;
import com.course.server.dto.ChapterDto;
import com.course.server.dto.PageDto;
import com.course.server.service.ChapterService;
import com.course.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 章控制器
 *
 * @author Feel
 * @date 2021/11/04
 */
@Slf4j
@RestController
@RequestMapping("/admin/chapter")
public class ChapterController extends ResultController {

    @Autowired
    private ChapterService chapterService;


    /**
     * 分页
     *
     * @param pageDto 页面 dto
     * @return {@link PageDto}<{@link ChapterDto}>
     */
    @PostMapping("/list")
    public ResponseDto<PageDto<ChapterDto>> chaterts(@RequestBody PageDto<ChapterDto> pageDto) {
        chapterService.list(pageDto);
        return success(pageDto);
    }

    /**
     * 保存
     *
     * @param chapterDto 章dto
     * @return {@link ChapterDto}
     */
    @PostMapping("/save")
    public ResponseDto<ChapterDto> save(@RequestBody ChapterDto chapterDto) {
        // 保存校验
        ValidatorUtil.require(chapterDto.getName(), "名称");
        ValidatorUtil.require(chapterDto.getCourseId(), "课程ID");
        ValidatorUtil.length(chapterDto.getCourseId(), "课程ID", 1, 8);

        chapterService.saveOrUpdate(chapterDto);
        return success(chapterDto);
    }

    /**
     * 删除一个
     *
     * @param id id
     * @return {@link ResponseDto}
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto deleteOne(@PathVariable String id) {
        chapterService.deleteById(id);
        return success();
    }

}
